﻿using System;
using System.Windows.Controls;
using Inovout.Gui.Wpf.ViewModels;
using Inovout.Descriptors;
using System.Windows.Data;

namespace Inovout.Gui.Wpf.Controls
{
    public class XDataGrid : DataGrid
    {
        public XDataGrid()
        {
            base.AutoGenerateColumns = false;
            this.IsReadOnly = true;
        }
        protected override void OnInitialized(EventArgs e)
        {
            base.OnInitialized(e);
            if (this.DataContext is DomainViewModel)
            {
                DomainViewModel viewModel = this.DataContext as DomainViewModel;
                ClassDescriptor modelDescriptor = 
                    ClassDescriptor.Create(viewModel.ViewData.Model.GetType().GetGenericArguments()[0]);
                foreach (var column in this.Columns)
                {
                    Binding binding = ((DataGridBoundColumn)column).Binding as Binding;
                    column.Header = modelDescriptor.GetProperty(binding.Path.Path).Alias;
                }
            }
        }
    }
}
